package com.tangruitao.controller;



import com.tangruitao.bean.Detail;
import com.tangruitao.bean.Users;
import com.tangruitao.bean.Zhuce;
import com.tangruitao.service.ZhuceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.UUID;

/**
 * 这个类的作者是13536
 * 开发日期:2021/12/27
 **/
@Controller
public class MyController {

    @Autowired
    ZhuceService zhuceService;
    @ResponseBody
    @RequestMapping("photo")
    public String photo(MultipartFile file) throws IOException {
        if(file!=null){
            String sub = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
            String s = UUID.randomUUID().toString();
            String newfile=s+sub;
            file.transferTo(new File("d:/pic/",newfile));
            return newfile;
        }
        return "";
    }

    @RequestMapping("tozhuce")
    public String tozhuce(){
        return "zhuce";
    }

    @RequestMapping("zhuce")
    public String zhuce(Zhuce zhuce){
        Integer i = zhuceService.insertZhuce(zhuce);
        return "redirect:tologin";
    }

    @RequestMapping("tologin")
    public String tologin(HttpServletRequest request){
        request.setAttribute("user",new Users());
        return "login";
    }
    @RequestMapping("login")
    public String tologin(HttpSession session, @Validated @ModelAttribute("user") Users users, BindingResult result){
        if(result.hasErrors()){
            return "login";
        }
        Users users1 = zhuceService.selectUserByName(users.getUname());
        if(users1==null){
            result.rejectValue("uname","","账号错误");
            return "login";
        }else{
            if(users1.getUpassword().equals(users.getUpassword())){
                Detail de = new Detail("109.90.09", "四川安阳", new Date(), users1.getUname());
                zhuceService.insertDatil(de);
                session.setAttribute("USERS",users1);
                return "redirect:toxianqi";
            }else{
                result.rejectValue("upassword","","密码错误");
                return "login";
            }
        }

    }
    @RequestMapping("toxianqi")
    public String toxianqi(HttpServletRequest request){
        Users users =(Users) request.getSession().getAttribute("USERS");
        if(users!=null){
            Zhuce zhuce =zhuceService.selectZhuceByUid(users.getUname());
            request.setAttribute("zhu",zhuce);
            return "xianqi";
        }
        return "";
    }

}
